package com.hspedu.seckill.rabbitmq;

import com.hspedu.seckill.config.RabbitMQConfig;
import com.hspedu.seckill.config.RabbitMQHeadersConfig;
import com.hspedu.seckill.config.RabbitMQTopicConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: xujiabing
 * @date: 2024-11-24 17:48
 * @description: 消息发送者/生产者
 */
@Service
@Slf4j
public class MQSender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息 -> 指定队列
     */
    public void send(Object msg) {
        log.info("发送的消息是 = {}", msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE, msg);
    }

    /**
     * 发送消息 -> 指定交换机
     * routingKey 为空，表示广播队列
     * @param msg
     */
    public void sendFanout(Object msg) {
        log.info("发送消息 = {}" , msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE,"", msg);
    }

    /**
     * 发送消息 -> 指定交换机
     * 路由模式：将消息发送到directExchange 交换机,同时指定路由queue.red
     * @param msg
     */
    public void sendDirect1(Object msg) {
        log.info("发送消息 = {}" , msg);
        //
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_DIRECT,
                RabbitMQConfig.rountingkey01,
                msg
        );
    }

    /**
     * 发送消息 -> 指定交换机
     * 路由模式：将消息发送到directExchange 交换机,同时指定路由queue.red
     * @param msg
     */
    public void sendDirect2(Object msg) {
        log.info("发送消息 = {}" , msg);
        //
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_DIRECT,
                RabbitMQConfig.rountingkey02,
                msg
        );
    }

    /**
     * 发送消息 -> 指定交换机(topic模式)
     * @param msg
     */
    public void sendTopic01(Object msg) {
        log.info("发送消息sendTopic01 = {}" ,msg);
        //发送消息到topicExchange 队列，同时携带routingKey "queue.red.message"
        rabbitTemplate.convertAndSend(
                RabbitMQTopicConfig.EXCHANGE,
                "queue.red.message",
                msg
        );
    }

    /**
     * 发送消息 -> 指定交换机(topic模式)
     * @param msg
     */
    public void sendTopic02(Object msg) {
        log.info("发送消息sendTopic02 = {}" ,msg);
        //发送消息到topicExchange 队列，同时携带routingKey green.queue.green.message
        rabbitTemplate.convertAndSend(
                RabbitMQTopicConfig.EXCHANGE,
                "green.queue.green.message",
                msg
        );
    }

    /**
     * 发送消息 -> header交换机,并携带指定的 k-v
     * @param msg
     */
    public void sendHeader01(String msg) {
        log.info("发送消息sendHeader01 = {}" ,msg);
        //创建消息属性
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "fast");
        //创建Message对象
        Message message = new Message(msg.getBytes(), properties);
        rabbitTemplate.convertAndSend(
                RabbitMQHeadersConfig.EXCHANGE,
                "",
                message
        );
    }

    /**
     * 发送消息 -> header交换机,并携带指定的 k-v
     * @param msg
     */
    public void sendHeader02(String msg) {
        log.info("发送消息sendHeader02 = {}" ,msg);
        //创建消息属性
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed", "normal");
        //创建Message对象
        Message message = new Message(msg.getBytes(), properties);
        rabbitTemplate.convertAndSend(
                RabbitMQHeadersConfig.EXCHANGE,
                "",
                message
        );
    }


}
